// 写入到每日报表中
import 'dart:io';
import 'dart:isolate';

import 'package:excel/excel.dart' as top_excel;

import '../bean/DailyInformationBean.dart';

writeDailyReport(dynamic margeMap) {
  SendPort sendPort = margeMap["sendPort"]; // 端口
  File mainFile = margeMap["mainFile"]; // 主文件
  DailyInformationBean waybillPrintingBean = margeMap["waybillPrintingBean"]; // 需要合并的数据
  String sheetName = margeMap["sheetName"] ?? "每天資料"; // sheet名称  默认是 Main 表名
  // 打开要合并的文件
  late top_excel.Excel excelMain;
  // try {
  var readAsBytesSync = mainFile.readAsBytesSync();
  excelMain = top_excel.Excel.decodeBytes(readAsBytesSync);

  var cmsMainSheet = excelMain.sheets[sheetName];
  if (cmsMainSheet == null) {
    sendPort.send({"code": 1, "msg": "没有找到$sheetName表"});
    return;
  }

  List<List<dynamic>> originData = [];
  waybillPrintingBean.data?.forEach((e) {
    originData.add([e.bLAPPLYDATE, e.cMCODE, e.bLCOST, e.bLBILLNUM, e.bLPCOST, e.bGNAME, e.bLVALIDITY, e.cMNAME, e.cMADDRESS]);
  });

  // 开始写入数据
  originData.asMap().forEach((index, value) {
    List<top_excel.CellValue> list = [];
    for (var childValue in originData[index]) {
      // 文本
      var textCellValue = top_excel.TextCellValue(childValue.toString());
      // topExcel.CellValue cellValue = textCellValue;
      list.add(textCellValue);
    }
    cmsMainSheet.appendRow(list);
    // int lastRowIndex = cmsMainSheet.maxRows - 1;
    // // 为新添加的行设置边框
    // top_excel.Border border = top_excel.Border(borderStyle: top_excel.BorderStyle.Thin, borderColorHex: top_excel.ExcelColor.black);
    // top_excel.CellStyle cellStyle = top_excel.CellStyle(fontSize: 22, fontColorHex: top_excel.ExcelColor.green, topBorder: border, bottomBorder: border, leftBorder: border, rightBorder: border);
    // for (int i = 0; i < (cmsMainSheet.rows[lastRowIndex].length); i++) {
    //   // 设置新增的单元格样式，包括边框和字体颜色
    //   cmsMainSheet.rows[lastRowIndex][i]?.cellStyle = cellStyle;
    // }
    // for (var cell in cmsMainSheet.rows[lastRowIndex]) {
    //   cell?.cellStyle = cellStyle;
    // }
    // 更新一下UI 界面进度
    // sendPort.send({"code": 2, "msg": "正在写入数据，请稍后... 当前处理的是第 ${index + 1} 行，一共 ${originData.length} 行", "value": (index + 1) / originData.length});
  });

  top_excel.Border border = top_excel.Border(borderStyle: top_excel.BorderStyle.Thin, borderColorHex: top_excel.ExcelColor.black);
  top_excel.CellStyle cellStyle = top_excel.CellStyle(fontSize: 22, fontColorHex: top_excel.ExcelColor.green, topBorder: border, bottomBorder: border, leftBorder: border, rightBorder: border);

  // 写入汇总的数据
  cmsMainSheet.appendRow([top_excel.TextCellValue(""), top_excel.TextCellValue("12/2024 月份套票銷售總額"), top_excel.TextCellValue("售價"), top_excel.TextCellValue("套票數量"), top_excel.TextCellValue("平均價/票")]);
  // 添加边框
  cmsMainSheet.appendRow([top_excel.TextCellValue(""), top_excel.TextCellValue(""), top_excel.TextCellValue("100"), top_excel.TextCellValue("20"), top_excel.TextCellValue("5.00")]);
  cmsMainSheet.appendRow(
      [top_excel.TextCellValue(""), top_excel.TextCellValue("CXC 直客"), top_excel.TextCellValue("100"), top_excel.TextCellValue("20"), top_excel.TextCellValue("5.00"), top_excel.TextCellValue("5%")]);
  cmsMainSheet.appendRow(
      [top_excel.TextCellValue(""), top_excel.TextCellValue("加盟商"), top_excel.TextCellValue("10"), top_excel.TextCellValue("2"), top_excel.TextCellValue("1.00"), top_excel.TextCellValue("1%")]);

  // 写入之后进行保存到他本身
  final bytes = excelMain.encode();
  dynamic result = {};
  try {
    sendPort.send({"code": 2, "msg": "正在写入到文件中，请稍后..."});
    mainFile.writeAsBytesSync(bytes!);
    result = {"code": 0, "msg": "写入成功"};
    sendPort.send(result);
  } catch (e) {
    print(e.toString());
    if (e is PathAccessException) {
      result = {"code": 1, "msg": "文件被占用"};
    }
  }
}
